<?php
namespace app\admin\controller;
use app\admin\model\Customers;
use app\admin\model\Users;
use app\admin\controller\AdminAuth;
use think\Validate;
use think\Image;
use think\Db;
use think\config;
use think\Request;
use app\admin\collection\AliyunOss;

class NoticeController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '信息公告',
		'module_url'  => '/admin/notice/',
		'module_slug' => 'notice',
		'upload_path' => UPLOAD_PATH,
		'upload_url'  => '/public/uploads/',
    );
	//信息公告列表
    public function index(){
        $notice = Db::name('notice')
            ->alias('N')
            ->join('users U','N.user_id = U.id')
            ->field('N.*,U.nickname')
            ->order('notice_id DESC')
            ->paginate(10);

        $this->assign([
            'data' => $this->data,
            'notice' => $notice
        ]);
        return view();
    }
    //公告添加
    public function create(){
        $data = input('post.');
        $data['addtime'] = time();
        $data['user_id'] = session('uid');
        if(!session('uid')){
            return $this->error('登录异常！');
        }
        $notice = Db::name('notice')->insert($data);
        if($notice){
            return $this->success('发布成功',$this->data['module_url'],'','1');
        }else{
            return $this->error('发布失败',$this->data['module_url'],'','1');
        }
    }
    //删除公告
    public function delete(){
        $notice_id = input('notice_id');
        $data['notice_id'] = $notice_id;
        if ($notice_id) {
            Db::name('notice')->where(['notice_id'=>$notice_id])->delete();  //删除
            $data['error'] = 0;
            $data['msg'] = '删除成功';
        } else {
            $data['error'] = 1;
            $data['msg'] = '删除失败';
        }
        return $data;
    }

    //公告详情
    public function noticedetail(){
        $notice_id = input('notice_id');
        $notice = Db::name('notice')->where(['notice_id'=>$notice_id])->find();
        return json($notice);
    }

    //公告更新
    public function upNotice(){
        $data = input('post.');
        $upNotice = Db::name('notice')->update($data);
        if($upNotice){
            return $this->success('修改成功',$this->data['module_url'],'','1');
        }else{
            return $this->error('修改失败',$this->data['module_url'],'','1');
        }
    }

}